#pragma once

#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include "Mutex.hpp"
#include "Cond.hpp"
#include "Thread.hpp"
#include "Log.hpp"

namespace ThreadPoolModule
{
    using namespace LockModule;
    using namespace CondModule;
    using namespace ThreadModule;
    using namespace LogModule;

    using thread_t = std::shared_ptr<Thread>;
    static const int defaultnum = 5;

    void DefaultTest()
    {
        while (true)
        {
            LOG(LogLevel::DEBUG) << "我是一个测试方法";
            sleep(1);
        }
    }

    template<typename T>
    class ThreadPool
    {
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程:" << name << "进入HandlerTask逻辑";
            while (true)
            {
                // 1.获取任务
                T t;
                {
                    LockGuard lockguard(_lock);
                    while (IsEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }
                    if (IsEmpty() && !_isrunning)
                        break;
                    t = _taskq.front();
                    _taskq.pop();
                }

                // 2.处理任务
                t(name);
            }
            LOG(LogLevel::INFO) << "线程:" << name << "退出HandlerTask逻辑";
        }

        bool IsEmpty() { return _taskq.empty(); }
    public:
        ThreadPool(int num = defaultnum)
        :_num(num)
        ,_wait_num(0)
        ,_isrunning(false)
        {
            for (int i = 0; i < _num; i++)
            {
                _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1)));
                LOG(LogLevel::INFO) << "构建线程" << _threads[i]->Name() << "对象...成功";
            }
        }

        void Start()
        {
            if (_isrunning) return;
            _isrunning = true;
            for (auto& thread_ptr : _threads)
            {
                thread_ptr->Start();
                LOG(LogLevel::INFO) << "启动线程" << thread_ptr->Name() << "...成功";
            }
        }

        void Wait()
        {
            for (auto& thread_ptr : _threads)
            {
                thread_ptr->Join();
                LOG(LogLevel::INFO) << "回收线程" << thread_ptr->Name() << "...成功";
            }
        }
        
        void Equeue(const T& in)
        {
            LockGuard lockguard(_lock);
            if (!_isrunning) return;
            _taskq.push(std::move(in));
            if (_wait_num > 0)
                _cond.Notify();
        }

        void Stop()
        {
            LockGuard lockguard(_lock);
            if (_isrunning)
            {
                _isrunning = false;
                if (_wait_num > 0)
                    _cond.NotifyAll();
            }
        }

        ~ThreadPool()
        {}
    private:
        std::vector<thread_t> _threads;   // 线程智能指针数组
        int _num;                         // 线程数量
        std::queue<T> _taskq;             // 任务队列
        Mutex _lock;                      // 互斥量
        Cond _cond;                       // 条件变量
        int _wait_num;                    // 在条件变量下等待的线程数
        bool _isrunning;                  // 线程池是否启动
    };
}
